/**
 * =================================================================
 * 版权所有 2011-2013 深圳市泰海网络科技服务有限公司，并保留所有权利
 * -----------------------------------------------------------------
 * 这不是一个自由软件！您不能在任何未经允许的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布
 * =================================================================
 */
package com.sfpay.api.internal.util;

import java.io.IOException;

import com.sfpay.api.ApiRuleException;
import com.sfpay.api.FileItem;


/**
 * 类说明：参数验证工具类<br>
 * 
 * <p>
 * 详细描述：<br>
 * 
 * </p>
 * <pre>
 * ——————————————————————————————————————————————————————————————————
 * |		修改人		|		修改时间			|		修改原因
 * ——————————————————————————————————————————————————————————————————
 * |	354190 曾宪新	|		2013-10-18		|	
 * ——————————————————————————————————————————————————————————————————
 * </pre>
 * 
 * @author 354190 曾宪新(Xavier.zeng)
 * 
 * CreateDate: 2013-10-18
 */
public class RequestCheckUtils {

	public static final String ERROR_CODE_ARGUMENTS_MISS = "40";// Missing 	// Required // Arguments

	public static final String ERROR_CODE_ARGUMENTS_INVALID = "41";// Invalid // Arguments

	public static void checkNotEmpty(Object value, String fieldName)
			throws ApiRuleException {
		if (value == null) {
			throw new ApiRuleException(ERROR_CODE_ARGUMENTS_MISS, "client-error:Missing Required Arguments:" + fieldName + "");
		}
		if (value instanceof String) {
			if (((String) value).trim().length() == 0) {
				throw new ApiRuleException(ERROR_CODE_ARGUMENTS_MISS, "client-error:Missing Required Arguments:" + fieldName + "");
			}
		}
	}

	public static void checkMaxLength(String value, int maxLength,
			String fieldName) throws ApiRuleException {
		if (value != null) {
			if (value.length() > maxLength) {
				throw new ApiRuleException(ERROR_CODE_ARGUMENTS_INVALID, "client-error:Invalid Arguments:the length of " + fieldName + " can not be larger than " + maxLength + ".");
			}
		}
	}

	public static void checkMaxLength(FileItem fileItem, int maxLength,
			String fieldName) throws ApiRuleException {
		try {
			if (fileItem != null && fileItem.getContent() != null) {

				if (fileItem.getContent().length > maxLength) {
					throw new ApiRuleException(ERROR_CODE_ARGUMENTS_INVALID, "client-error:Invalid Arguments:the length of " + fieldName + " can not be larger than " + maxLength + ".");
				}
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static void checkMaxListSize(String value, int maxSize,
			String fieldName) throws ApiRuleException {
		if (value != null) {
			String[] list = value.split(",");
			if (list != null && list.length > maxSize) {
				throw new ApiRuleException(ERROR_CODE_ARGUMENTS_INVALID, "client-error:Invalid Arguments:the listsize(the string split by \",\") of " + fieldName + " must be less than " + maxSize + ".");
			}
		}
	}

	public static void checkMaxValue(Long value, long maxValue, String fieldName)
			throws ApiRuleException {
		if (value != null) {
			if (value > maxValue) {
				throw new ApiRuleException(ERROR_CODE_ARGUMENTS_INVALID, "client-error:Invalid Arguments:the value of " + fieldName + " can not be larger than " + maxValue + ".");
			}
		}
	}

	public static void checkMinValue(Long value, long minValue, String fieldName)
			throws ApiRuleException {
		if (value != null) {
			if (value < minValue) {
				throw new ApiRuleException(ERROR_CODE_ARGUMENTS_INVALID, "client-error:Invalid Arguments:the value of " + fieldName + " can not be less than " + minValue + ".");
			}
		}
	}
}
